package com.sz.common.base.dao.schema;

import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Package: com.sz.common.base.dao.schema <br>
 * Function: TODO: ADD FUNCTION <br>
 * Author: Charles. <br>
 * Date: 2017-02-21 14:24:00.
 */
public class TableSchema {
    private String tableName;
    private List<String> primaryKeyNames = new ArrayList<>();
    private List<FieldSchema> fields = new ArrayList<>();

    /**
     * @param tableName
     */
    public TableSchema(String tableName) {
        this(tableName, "id");
    }

    /**
     * @param tableName
     */
    public TableSchema(String tableName, String primaryKey) {
        this.tableName = tableName;
        this.primaryKeyNames.add(primaryKey);
    }

    /**
     * @return
     */
    public String getTableName() {
        return tableName;
    }

    /**
     * @param tableName
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getPrimaryKeyName() {
        if (primaryKeyNames.size() > 0) {
            return primaryKeyNames.get(0);
        }
        return "";
    }

    public String[] getPrimaryKeyNames() {
        return this.primaryKeyNames.toArray(new String[]{});
    }

    public void addPrimaryKeyName(String primaryKeyName) {
        if (StringUtils.hasLength(primaryKeyName)
                && !this.primaryKeyNames.contains(primaryKeyName)) {
            this.primaryKeyNames.add(primaryKeyName);
        }
    }

    public List<FieldSchema> getFields() {
        return fields;
    }

    /**
     * @param fieldName
     * @return
     */
    public FieldSchema getField(String fieldName) {
        if (StringUtils.hasLength(fieldName) && this.fields != null && fields.size() > 0) {
            for (FieldSchema field : fields) {
                if (fieldName.equalsIgnoreCase(field.getFieldName())) {
                    return field;
                }
            }
        }
        return null;
    }
}
